<?php
class App_Model extends CI_Model{

	public function __construct(){
		parent::__construct();
		$this->db 		= $this->load->database('default', TRUE);
	}
	public function getCateGory(){
		$this->db->select('cat_type_id, cat_type_name');
		$this->db->where('cat_type_delete_flag',0);
		$this->db->order_by("cat_sort_order", "asc");
		$data = $this->db->get('cat_type')->result_array();
		return $data;
	}
	public function getImagesBannerSub(){
		$this->db->select('images, colums');
		$this->db->where('delete_flag',0);
		$this->db->order_by("colums",'asc'); 
		$data = $this->db->get('image_subbanner')->result_array();
		return $data;
	}
	public function getCateGory_SubId($id=null){
		$this->db->select('cat_type_sub_id, cat_type_sub_name, images');
		$this->db->where(array('cat_type_sub_delete_flag'=>0,'cat_type_id'=>$id));
		$this->db->order_by("cat_type_sub_sort_order", "asc");
		$data = $this->db->get('cat_type_sub')->result_array();
		return $data;
	}
	public function getCateGory_SubCountId($id=null){
		$sql = 'select count(cat_type_sub_id) as count from common_category where delete_flag = 0 and cat_type_sub_id=?';
		$data = $this->db->query($sql,array($id))->result_array();
		return $data[0]['count'];
	}
	public function getSubcateId($ref = null,$limit = 10, $offset = 0){
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$ref));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('common_category',$limit, $offset)->result_array();
		return $data;
//		$sql = 'SELECT common_category.cat_id, common_category.cat_name, common_category.images, cat_type_sub_name
//				FROM cat_type_sub, common_category
//				WHERE delete_flag =0
//				AND common_category.cat_type_sub_id ='.$ref.'
//				AND cat_type_sub.cat_type_sub_id = common_category.cat_type_sub_id
//				ORDER BY common_category.cat_type_sub_sort_order ASC , common_category.update_date DESC';
//		$data = $this->db->query($sql)->result_array();
//		return $data;
	}
	public function getCateGoryName_SubId($id=null){
		$this->db->select('cat_type_sub_name');
		$this->db->where(array('cat_type_sub_delete_flag'=>0,'cat_type_sub_id'=>$id));
		$data = $this->db->get('cat_type_sub')->result_array();
		return $data[0]['cat_type_sub_name'];
	}
	public function count_all($id = null){
		$data = $this->db->get_where('common_category',array('cat_type_sub_id'=>$id,'delete_flag'=>0));
		return $data->num_rows();
	}
	public function showDetailId($ref = null){
		$this->db->select('cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('common_category')->result_array();
		return $data;
	}
	public function showDetailIdMember($ref = null){
		$this->db->select('cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('member_category')->result_array();
		return $data;
	}
	public function showRelationId($ref = null){
		//ten cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price, cat_id, cat_type_sub_id
		$this->db->select('cat_type_sub_id');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('common_category')->result_array();
		$sub_id = $data[0]['cat_type_sub_id'];
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$sub_id));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('common_category',12, 0)->result_array();
		return $data;
	}
	public function showRelationIdMember($ref = null){
		//ten cat_name, images, cat_description, cat_demension, cat_assembly, cat_material, cat_color, cat_price, cat_id, cat_type_sub_id
		$this->db->select('cat_type_sub_id');
		$this->db->where(array('delete_flag'=>0,'cat_id'=>$ref));
		$data = $this->db->get('common_category')->result_array();
		$sub_id = $data[0]['cat_type_sub_id'];
		$this->db->select('cat_id, cat_name, images,cat_price,cat_size');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$sub_id));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$data = $this->db->get('member_category',12, 0)->result_array();
		return $data;
	}
//dung de dem so luong nguoi online start 20130730
	public function saveUserCount($ref = null){
		if ($this->checkUserOnline($ref)){
			$this->db->update('useronline', array('time_user'=>$ref['time_user']), array('ip_user'=>$ref['ip_user'],'local_user'=>$ref['local_user'],'date'=>$ref['date'],'sess_id'=>$ref['sess_id']));
		}else{
			$this->db->insert('useronline', $ref);
		}
		return $this->db->affected_rows();
	}
	private function checkUserOnline($data = null){
		$this->db->select('ip_user,local_user');
		$this->db->where(array('local_user'=>$data['local_user'],'ip_user'=>$data['ip_user'],'date'=>$data['date'],'sess_id'=>$data['sess_id']));
		$data = $this->db->get('useronline');
		return $data->num_rows();
	}
	public function deleteOldUser($ref = null){
		$sql="delete from useronline where time_user <".$ref;
		$this->db->query($sql);
		return $this->db->affected_rows();
	}
	public function getOldUser($ref = null,$tg = null){
		$time = $tg - 900;
		$sql = '';
//		$sql.= " select distinct ip_user from useronline where local_user ='".$ref."'";//chi can vao trang web la tinh khong can dem online cung trang nen bo dieu kien nay
		$sql.= ' select distinct ip_user,sess_id from useronline where time_user >= '.$time;
		$data = $this->db->query($sql);
//		$this->db->distinct();
//		$this->db->select('ip_user');
//		$this->db->where(array('local_user'=>$ref));
//		$data = $this->db->get('useronline');
		return $data->num_rows();
	}
	public function getTodayUser($ref = null){
		$this->db->distinct();
		$this->db->select('ip_user,sess_id');
		$this->db->where(array('date'=>$ref));
		$data = $this->db->get('useronline');
		return $data->num_rows();
	}
	public function getDateUser($ref_start = null,$ref_end=null){
		$sql = '';
		$sql.= " select distinct ip_user,sess_id, date from useronline where date BETWEEN '".$ref_start."'";
		$sql .=" and '".$ref_end."'";
		$data = $this->db->query($sql);
		return $data->num_rows();
	}
	public function getTotalUser(){
		$sql  ='';
		$sql .= 'select distinct ip_user,sess_id, date from useronline';
		$data = $this->db->query($sql);
		return $data->num_rows();
	}
	public function getMaxId(){
		$sql  ='';
		$sql .= 'select max(id) as num from useronline';
		$data = $this->db->query($sql)->result_array();
		return $data[0]['num'];
	}
	public function convertNum2Img($str = 0){
		$count = strlen($str);
		if ($count<5){
			$add = 5 - $count;
			for ($j=0;$j<$add;$j++){
				$str = '0'.$str;
			}
		}
		$arr = str_split($str);
		return $arr;
	}
	public function getImgTypeSub($sub_id){
//		$sql  ='';
//		$sql .= 'select images from common_category where delete_flag = 0 and cat_type_sub_id = '.$sub_id;
//		$sql .= ' order by cat_type_sub_sort_order asc, update_date desc';
		$this->db->select('images');
		$this->db->from ('common_category');
		$this->db->where(array('delete_flag'=>0,'cat_type_sub_id'=>$sub_id));
		$this->db->order_by("cat_type_sub_sort_order asc, update_date desc");
		$this->db->limit('1');
		$data = $this->db->get()->result_array();
		if (count($data)>0) return $data['0']['images'];
		else return 'updateimg.jpg';
	}
	//dung de dem so luong nguoi online end 20130730
//	public function getTypeSub($ref =null){
//		$this->db->select('cat_type_sub_id, cat_type_sub_name');
//		$this->db->where(array('cat_type_sub_delete_flag'=>0,'cat_type_id'=>$ref));
//		$data = $this->db->get('cat_type_sub')->result_array();
//		return $data;
//	}
//	public function getType(){
//		$this->db->select('cat_type_id, cat_type_name');
//		$this->db->where(array('cat_type_delete_flag'=>0));
//		$data = $this->db->get('cat_type')->result_array();
//		return $data;
//	}
}